package com.scfinance.SPD.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by admin on 2017/7/13.
 */
public class ZipUtil {

    public static final String SPDUPLOAD_TYPE_FANGKUAN = "10";  //发放款凭证的浦发导出
    public static final String SPDUPLOAD_TYPE_YUSHEN   = "20";  //发预审函的浦发导出

    private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);

    public static String buildZip(String appId, Map<String, Object> map) throws Exception {
        byte[] buffer = new byte[1024];
        String strZipName = PropsUtil.getProperty("uploadZip.fileUrl") + appId + ".zip";
        File zipFile = new File(strZipName);
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        String fileName = map.get("id_card_front") + "," + map.get("id_card_back") + "," + map.get("applicationContract") + "," + map.get("groupPhoto");
        //根据不同的进件方式提供不同的影像件：1.需要放款凭证2.需要预审函
        if (SPDUPLOAD_TYPE_FANGKUAN.equals(map.get("uploadType"))) {
            fileName = fileName + "," + map.get("loanVoucher") + "," + map.get("pretrial");
        }else if (SPDUPLOAD_TYPE_YUSHEN.equals(map.get("uploadType"))) {
            fileName = fileName + "," + map.get("pretrial");
        }
        String[] Imgs = fileName.split(",");
        String loanOrderId = (String) map.get("order_id");
        logger.info("***BUG开始重名图片***");
        File sourceFile = ImageUtil.renameImage(Imgs, appId, loanOrderId);
        File[] sourceFileList = sourceFile.listFiles();
        for (File file : sourceFileList) {
            FileInputStream fis = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(file.getName()));
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            fis.close();
        }
        zipOut.close();
        FileUtil.deleteDirectory(sourceFile.getPath());  //删除文件夹
        return strZipName;
    }

    public static String buildZipTest(String appId) throws Exception {
        byte[] buffer = new byte[1024];
        String strZipName = "d:/" + appId + ".zip";
        File zipFile = new File(strZipName);
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        File sourceFile = ImageUtil.renameImageTest(appId);  //测试方法
        File[] sourceFileList = sourceFile.listFiles();
        for (File file : sourceFileList) {
            FileInputStream fis = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(file.getName()));
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            fis.close();
        }
        zipOut.close();
        FileUtil.deleteDirectory(sourceFile.getPath());  //删除文件夹
        return strZipName;
    }

    public static String buildZipWithPath(String path, Map<String, Object> map) throws Exception {
        String appId = (String) map.get("appId");
        byte[] buffer = new byte[1024];
        String strZipName = PropsUtil.getProperty("uploadZip.fileUrl") + appId + ".zip";
        File zipFile = new File(strZipName);
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        File sourceFile = ImageUtil.renameImageAlone(path, appId);
        File[] sourceFileList = sourceFile.listFiles();
        for (File file : sourceFileList) {
            FileInputStream fis = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(file.getName()));
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zipOut.write(buffer, 0, len);
            }
            zipOut.closeEntry();
            fis.close();
        }
        zipOut.close();
        return strZipName;
    }
}
